package com.ming.stringstudy.feedbacksystem.controller;

import com.ming.stringstudy.feedbacksystem.entity.CommentType;
import com.ming.stringstudy.feedbacksystem.pojo.PageBean;
import com.ming.stringstudy.feedbacksystem.pojo.Result;
import com.ming.stringstudy.feedbacksystem.service.ICommentTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 问题/意见类型表 前端控制器
 * </p>
 *
 * @author aming
 * @since 2024-12-29
 */
@RestController
@RequestMapping("/commentType")
public class CommentTypeController {
    @Autowired
    private ICommentTypeService commentTypeService;

    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @RequestParam(required = false) Integer id,
                       @RequestParam(required = false) Integer deptId,
                       @RequestParam(required = false) String type){
        try {
            CommentType commentType = new CommentType();
            commentType.setId(id);
            commentType.setDeptId(deptId);
            commentType.setType(type);
            PageBean pageBean = commentTypeService.page(page, pageSize, commentType);
            return Result.success(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }

    }

    @PostMapping
    public Result addCommentType(@RequestBody CommentType commentType) {
        try {
            int result = commentTypeService.addCommentType(commentType);
            if (result == 0) {
                return Result.error("添加失败");
            } else {
                return Result.success("添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务器内部错误");
        }
    }

    @PutMapping
    public Result updateCommentType(@RequestBody CommentType commentType) {
        try {
            int result = commentTypeService.updateCommentType(commentType);
            if (result == 0) {
                return Result.error("修改失败");
            } else {
                return Result.success("修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/{ids}")
    public Result deleteCommentType(@PathVariable List<Integer> ids){
        try {
            int result = commentTypeService.deleteCommentType(ids);
            if (result == 0) {
                return Result.error("删除失败");
            }else {
                return Result.success("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("服务器内部错误");
        }
    }
}
